<?php
namespace Admin\Controller;

use Think\Page;
use THink\Controller;

class ActivityController extends Controller
{
	protected $model = null;

	public function __construct()
	{
		parent::__construct();
		$this->model = M('Activity');
	}

	public function index()
	{
		$where = [];

        // 查找
        $title = I('get.title', 0, 'trim');
        if ($title) {

            $where['title']  = ['LIKE', '%' . $title . '%'];
            $fields['title'] = $title;
            $this->assign('title', $title);
        }

        $count = $this->model->where($where)->count();
        $this->assign('count', $count);

        $pages = new Page($count, C('PAGE'), $fields);
        $this->assign('pages', $pages->show());

        $datas = $this->model->where($where)->limit($pages->firstRow . ',' . $pages->listRows)->select();
        $this->assign('datas', $datas);

        $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
            '活动管理',
            '活动列表',
        ]));
        $this->display();
	}

	public function add()
	{
		if(IS_POST)
		{
        
			$data = [
                'category_id'   => I('post.category_id', 0, 'intval'),
                'title'        => I('post.title', '', 'trim'),
                'content'        => I('post.editorValue', '', 'trim'),
                'description' => I('post.description', '', 'trim'),
                'position'        => I('post.position', 0, 'intval'),
                'create_time'    => CUR_DATETIME,
            ];

            $result = upload('Activity_Image');

            $id = $this->model->add($data);

            if (isset($result['image'])) {
                $this->model
                    ->where(['id' => $id])
                    ->save(['image' => C('UPLOAD_DOMAIN') . $result['image']['url']]);
            }

            if ($id) {
                $this->success('添加成功', U(CONTROLLER_NAME . '/update', [
                    'id' => $id,
                ]));
            }
		}
		else
		{
			$category = M('activity_category')->field('id,name')->select();
			$this->assign('category', $category);

            $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
                '活动管理',
                '添加活动',
            ]));

			$this->display();
		}
		
	}

	public function update()
	{
		if(IS_POST)
		{
			$id   = I('post.id', 0, 'intval');

			$data = [
                'category_id'   => I('post.category_id', 0, 'intval'),
                'title'        => I('post.title', '', 'trim'),
                'content'        => I('post.editorValue', '', 'trim'),
                'description' => I('post.description', '', 'trim'),
                'position'        => I('post.position', 0, 'intval'),
            ];

            $result = upload('Activity_Image');

            $this->model->where(['id' => $id])->save($data);

            if (isset($result['image'])) {
                $this->model
                    ->where(['id' => $id])
                    ->save(['image' => C('UPLOAD_DOMAIN') . $result['image']['url']]);
            }

            $this->success('修改成功', U(CONTROLLER_NAME . '/update', [
                'id' => $id,
            ]));
		}
		else
		{
			$category = M('activity_category')->field('id,name')->select();
			$this->assign('category', $category);

            $id = I('get.id', 0, 'intval');

            $data = $this->model->find($id);
            $this->assign('data', $data);

            $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
                '分类管理',
                '分类修改',
            ]));

            $this->display();
		}
	}

	public function delete()
	{
		$id = I('post.id', 0, 'intval');

        $is = $this->model->where(['id' => $id,])->save(['is_delete' => 1]);

        if ($is)
        {
            success(['id' => $id,]);
        } else {
            error(101, '删除失败');
        }
	}



	public function category()
	{

		$where = [];

        // 查找
        $name = I('get.name', 0, 'trim');
        if ($name) {

            $where['name']  = ['LIKE', '%' . $name . '%'];
            $fields['name'] = $name;
            $this->assign('name', $name);
        }

        $count = M('activity_category')->where($where)->count();
        $this->assign('count', $count);

        $pages = new Page($count, C('PAGE'), $fields);
        $this->assign('pages', $pages->show());

        $datas = M('activity_category')->where($where)->limit($pages->firstRow . ',' . $pages->listRows)->select();
        $this->assign('datas', $datas);

        $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
            '分类管理',
            '分类列表',
        ]));
        $this->display();


	}

	public function category_add()
	{

		if(IS_POST)
		{
			$data = [
                'parent_id'   => I('post.category_id', '', 'trim'),
                'name'        => I('post.name', '', 'trim'),
                'description' => I('post.description', '', 'trim'),
                'sort'        => I('post.sort', 0, 'intval'),
            ];

            $id = $this->model->add($data);

            if ($id) {
                $this->success('添加成功', U(CONTROLLER_NAME . '/category_update', [
                    'id' => $id,
                ]));
            }
		}
		else
		{
			$category = M('activity_category')->field('id,name')->select();
			$this->assign('category', $category);

            $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
                '分类管理',
                '添加分类',
            ]));

			$this->display();
		}
	}

	public function category_update()
	{
		if(IS_POST)
		{
			$id   = I('post.id', 0, 'intval');
			$data = [
                'parent_id'   => I('post.category_id', '', 'trim'),
                'name'        => I('post.name', '', 'trim'),
                'description' => I('post.description', '', 'trim'),
                'sort'        => I('post.sort', 0, 'intval'),
            ];

            $id = M('activity_category')->where(['id'=>$id])->save($data);

            if ($id) {
                $this->success('修改成功', U(CONTROLLER_NAME . '/category_update', [
                    'id' => $id,
                ]));
            }
		}
		else
		{
			$category = M('activity_category')->field('id,name')->select();
			$this->assign('category', $category);


			$id = I('get.id', 0, 'intval');

            $data = M('activity_category')->find($id);
            $this->assign('data', $data);

            $this->assign('bread_crumb', $bread_crumb = generate_bread_crumb([
                '分类管理',
                '分类修改',
            ]));

            $this->display();
		}
		
	}


	public function category_delete()
	{

		$id = I('post.id', 0, 'intval');

        $is = M('activity_category')->where(['id' => $id,])->save(['is_delete' => 1]);

        if ($is)
        {
            success(['id' => $id,]);
        } else {
            error(101, '删除失败');
        }
	}

}